package gold.digger;

import gold.utils.InputUtil;

import java.util.*;

/**
 * Created by fanzhenyu02 on 2021/12/10.
 * common problem solver template.
 */
public class LC970 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public List<Integer> powerfulIntegers(int x, int y, int bound) {
            Set<Integer> xSet = new HashSet<>(Arrays.asList(1));
            Set<Integer> ySet = new HashSet<>(Arrays.asList(1));
            int xNum = x, yNum = y;

            while (xNum != 1 && xNum <= bound) {
                xSet.add(xNum);
                xNum *= x;
            }

            while (yNum != 1 && yNum <= bound) {
                ySet.add(yNum);
                yNum *= y;
            }

            Set<Integer> ans = new HashSet<>();
            for (Integer xt : xSet) {
                for (Integer yt : ySet) {
                    if (xt + yt > bound || ans.contains(xt + yt)) continue;
                    ans.add(xt + yt);
                }
            }

            return new ArrayList<>(ans);
        }
    }

    public void run() {
        Solution solution = new Solution();
        System.out.println(solution.powerfulIntegers(2, 3, 10));
        System.out.println(solution.powerfulIntegers(3, 5, 15));
        System.out.println(solution.powerfulIntegers(2, 1, 10));
    }

    public static void main(String[] args) throws Exception {
        LC970 an = new LC970();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
